package cn.edu.nju.oraldefense.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import cn.edu.nju.oraldefense.dao.GraduateDao;
import cn.edu.nju.oraldefense.dao.PaperDao;
import cn.edu.nju.oraldefense.dao.TeacherDao;
import cn.edu.nju.oraldefense.model.Graduate;
import cn.edu.nju.oraldefense.model.Paper;
import cn.edu.nju.oraldefense.model.Teacher;
import cn.edu.nju.oraldefense.service.GraduateService;

@Component("graduateService")
public class GraduateServiceImpl implements GraduateService {
	private GraduateDao graduateDao;
	private PaperDao paperDao;
	private TeacherDao teacherDao;

	public GraduateDao getGraduateDao() {
		return graduateDao;
	}

	@Resource
	public void setGraduateDao(GraduateDao graduateDao) {
		this.graduateDao = graduateDao;
	}

	public PaperDao getPaperDao() {
		return paperDao;
	}

	@Resource
	public void setPaperDao(PaperDao paperDao) {
		this.paperDao = paperDao;
	}

	public TeacherDao getTeacherDao() {
		return teacherDao;
	}

	@Resource
	public void setTeacherDao(TeacherDao teacherDao) {
		this.teacherDao = teacherDao;
	}

	public int getCount() {
		return graduateDao.findCount();
	}

	public int getCountByTeacher(Teacher teacher) {
		return graduateDao.findCountByTeacher(teacher);
	}

	public List getAll() {
		return graduateDao.findAll();
	}

	public Graduate getById(int id) {
		Graduate graduate = graduateDao.findById(id);
		if (graduate != null)
			graduate.setPaper(paperDao.findByGraduate(graduate));
		return graduate;
	}

	public Graduate getByNumber(String number) {
		List list = graduateDao.findByNumber(number);
		Graduate graduate = null;
		if (list != null && list.size() > 0) {
			graduate = (Graduate) list.get(0);
			graduate.setPaper(paperDao.findByGraduate(graduate));
		}
		return graduate;
	}

	public List getByPage(int start, int limit) {
		List<Graduate> gList = graduateDao.findByPage(start, limit);
		Graduate graduate = null;
		Paper paper = null;
		for (int i = 0; i < gList.size(); i++) {
			graduate = gList.get(i);
			paper = paperDao.findByGraduate(graduate);
			graduate.setPaper(paper);
		}
		return gList;
	}

	public List getByTeacherAndPage(Teacher teacher, int start, int limit) {
		List<Graduate> gList = graduateDao.findByTeacherAndPage(teacher, start,
				limit);
		Graduate graduate = null;
		Paper paper = null;
		for (int i = 0; i < gList.size(); i++) {
			graduate = gList.get(i);
			paper = paperDao.findByGraduate(graduate);
			graduate.setPaper(paper);
		}
		return gList;
	}

	public void addGraduate(Graduate g) {
		graduateDao.save(g);
	}

	public void removeGraduate(Graduate g) {
		graduateDao.delete(g);
	}

	public void modifyGraduate(Graduate g) {
		graduateDao.update(g);
	}

	public boolean assignTutor() {
		List<Graduate> gList = graduateDao.findAll();
		List<Teacher> tList = teacherDao.findAll();
		if (gList != null && tList != null && gList.size() > 0
				&& tList.size() > 0) {
			Graduate g;
			for (int i = 0; i < gList.size(); i++) {
				g = gList.get(i);
				g.setTeacher(tList.get(i % tList.size()));
				graduateDao.update(g);
			}
			return true;
		} else
			return false;
	}

	public List<Graduate> getAllGraduate() {
		List<Graduate> gList = graduateDao.findAll();
		Graduate graduate = null;
		Paper paper = null;
		for (int i = 0; i < gList.size(); i++) {
			graduate = gList.get(i);
			paper = paperDao.findByGraduate(graduate);
			graduate.setPaper(paper);
		}
		return gList;
	}

}
